
class EventBus {
    private events: { [key: string]: Function[] }
    constructor() {
        this.events = {};
    }

    on(eventName: string, callback: (...args: any[]) => void) {
        if (!this.events[eventName]) {
            this.events[eventName] = []
            this.events[eventName].push(callback)
        }
    }
    off(eventName: string, callback: Function) {
        if (this.events[eventName]) {
            this.events[eventName] = this.events[eventName].filter(cb => cb !== callback)
        }
    }
    emit(eventName: string, ...args: any[]) {
        if (this.events[eventName]) {
            this.events[eventName].forEach(cb => {
                cb(...args)
            })
        }
    }
    once(eventName: string, callback: Function) {
        const onceCallback = (...args: any[]) => {
            callback(...args)
            this.off(eventName, onceCallback)
        }
        this.on(eventName, onceCallback)
    }

}

export default new EventBus()